const Stack = require('./stack')

/*
let stack = new Stack()

stack.push('盘子1')
stack.push('盘子2')
stack.push('盘子3')

console.log(
    stack.pop()
)
console.log(
    stack.size()
)
*/

// 练习题
// 判断字符串是否为回文字符串

/*
    123321
    abccba
    123456
*/
const testSymmetricStr = (str) => {
    let result = false
    let stack = new Stack()

    for (let i = 0; i < str.length; i++) {
        const item = str[i]
        // 如果发现当前元素和栈顶元素相等
        if (stack.top() === item) {
            stack.pop() // 栈顶元素出栈
        } else {
            stack.push(item) // 元素压入栈中
        }
    }
    result = stack.isEmpty()
    return result
}
console.log('1. 判断字符串是否为回文字符串')
console.log(testSymmetricStr('123321')) // true
console.log(testSymmetricStr('abccba')) // true
console.log(testSymmetricStr('123456')) // false

console.log('2. 合法括号')

const isBracketsStr = (str) => {
    let result = false
    let stack = new Stack()

    for (let i = 0; i < str.length; i++) {
        const item = str[i]
        // 遇到左括号，入栈
        if (item === '(') {
            stack.push('(')
        } else if (item === ')') {
            if (stack.isEmpty()) {
                // 遇到右括号要先判断，如果栈为空。说明这是非法字符串
                return false
            }
            // 遇到右括号出栈
            stack.pop()
        }
    }
    result = stack.isEmpty()
    return result
}

console.log(isBracketsStr('1()(2(3))(4)')) // true
console.log(isBracketsStr('1122()22(1(22233),(22))(')) // false
console.log(isBracketsStr('1()(2(3)))(4)')) // false
console.log(isBracketsStr(')1()(2(3)))(4)')) // false

console.log('3. 浏览器前进后退')

class BrowserHistry {
    constructor() {
        this.a = new Stack()
        this.back = new Stack()
    }
    forward() { // 前进
        this.a.push(this.back.pop())
    }
    goBack() { // 后退
        this.back.push(this.a.pop())
    }
    to(url) {
        this.a.push(url)
        this.back.clear() // 清空前进的历史记录
    }
    // 测试：查看栈内容
    showStack() {
        console.log(this.a.items)
        console.log(this.back.items)
    }
}

let b = new BrowserHistry()

b.to('百度翻译')
b.to('百度')
b.to('B站')

b.goBack()
b.goBack()

b.forward()
b.forward()

// b.to('斗鱼')

b.showStack()

// 155. 最小栈
console.log('155. 最小栈')
class MinStack {
    constructor() {
        // 存储普通数据
        this.dataStack = new Stack()
        // 存储最小值
        this.minStack = new Stack()
    }
    push(x) {
        this.dataStack.push(x)
        // 判断当前值，是否比 minStack.top()
        if (x < this.minStack.top() || this.minStack.isEmpty()) {
            this.minStack.push(x)
        } else {
            this.minStack.push(this.minStack.top())
        }
    }
    pop() {
        this.minStack.pop()
        return this.dataStack.pop()
    }
    top() {
        return this.dataStack.top()
    }
    getMini() {
        return this.minStack.top()
    }
}

let minStack = new MinStack()

minStack.push(-2)
minStack.push(0)
minStack.push(-3)
console.log('-3: ',minStack.getMini())
minStack.pop()
console.log('0: ', minStack.top())
console.log('-2: ',minStack.getMini())
/*
    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin();   --> 返回 -3.
    minStack.pop();
    minStack.top();      --> 返回 0.
    minStack.getMin();   --> 返回 -2.
*/
